/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.procurement.service;

import java.util.Set;

import com.javaeye.common.util.DateUtils;
import com.javaeye.procurement.dao.MaterialsReceiveOrderDAO;
import com.javaeye.procurement.dao.StatementRecordDAO;
import com.javaeye.procurement.dto.MaterialsReceiveOrder;
import com.javaeye.procurement.dto.MaterialsReceiveOrderDetail;
import com.javaeye.procurement.dto.StatementRecord;
import com.javaeye.store.dao.MaterialsBatchDetailDAO;
import com.javaeye.store.dao.MaterialsChangeDetailDAO;
import com.javaeye.store.dao.MaterialsSKUDetailDAO;
import com.javaeye.store.dto.MaterialsBatchDetail;
import com.javaeye.store.dto.MaterialsChangeDetail;
import com.javaeye.store.dto.MaterialsSKUDetail;

public class MaterialsReceiveInStoreService implements IMaterialsReceiveInStoreService {
	private MaterialsChangeDetailDAO materialsChangeDetailDao;
	
	private MaterialsSKUDetailDAO materialsSKUDetailDao;
	
	private MaterialsBatchDetailDAO materialsBatchDetailDao;
	
	private MaterialsReceiveOrderDAO orderDao;
	
	private StatementRecordDAO statementRecordDao;
	
	public void setMaterialsChangeDetailDao(
			MaterialsChangeDetailDAO materialsChangeDetailDao) {
		this.materialsChangeDetailDao = materialsChangeDetailDao;
	}

	public void setMaterialsSKUDetailDao(MaterialsSKUDetailDAO materialsSKUDetailDao) {
		this.materialsSKUDetailDao = materialsSKUDetailDao;
	}

	public void setMaterialsBatchDetailDao(
			MaterialsBatchDetailDAO materialsBatchDetailDao) {
		this.materialsBatchDetailDao = materialsBatchDetailDao;
	}
	
	public void setOrderDao(MaterialsReceiveOrderDAO orderDao) {
		this.orderDao = orderDao;
	}
	
	public void setStatementRecordDao(StatementRecordDAO statementRecordDao) {
		this.statementRecordDao = statementRecordDao;
	}

	public void saveInStoreResult(MaterialsReceiveOrder order) {
		Set<MaterialsReceiveOrderDetail> details = order.getOrderDetails();
		for (MaterialsReceiveOrderDetail orderDetail : details) {
			int materialsId = orderDetail.getMaterialsId();
			float acceptNumber = orderDetail.getAcceptNumber();
			float otherNumber = orderDetail.getOtherNumber();
			
			//插入库存变动明细表
			if (acceptNumber > 0) {
				MaterialsChangeDetail changeDetail = new MaterialsChangeDetail();
				changeDetail.setMaterialsId(materialsId);
				changeDetail.setVoucherNo(order.getId());
				changeDetail.setChangeType(MaterialsChangeDetail.PROCUREMENT_QUALIFIED_INSTORE);
				changeDetail.setBatchNo(order.getId());
				changeDetail.setNumber(acceptNumber);
				materialsChangeDetailDao.save(changeDetail);
			}
			if (otherNumber > 0) {
				MaterialsChangeDetail changeDetail = new MaterialsChangeDetail();
				changeDetail.setMaterialsId(materialsId);
				changeDetail.setVoucherNo(order.getId());
				changeDetail.setChangeType(MaterialsChangeDetail.PROCUREMENT_UNQUALIFIED_INSTORE);
				changeDetail.setBatchNo(order.getId());
				changeDetail.setNumber(otherNumber);
				materialsChangeDetailDao.save(changeDetail);
			}
			
			//更新实时库存数量
			MaterialsSKUDetail skuDetail = materialsSKUDetailDao.getMaterialsSKUDetail(materialsId);
			if (acceptNumber > 0) {
				skuDetail.setNumber(skuDetail.getNumber() + acceptNumber);
			}
			if (otherNumber > 0) {
				skuDetail.setDefectiveNumber(skuDetail.getDefectiveNumber() + otherNumber);
			}
			skuDetail.setWaitNumber(skuDetail.getWaitNumber() - orderDetail.getNumber());
			materialsSKUDetailDao.saveMaterialsSKUDetail(skuDetail);

			//更新批次库存数量
			MaterialsBatchDetail batchDetail = 
				materialsBatchDetailDao.getBatchDetail(materialsId, order.getId());
			if (batchDetail == null) {
				batchDetail = new MaterialsBatchDetail();
				batchDetail.setMaterialsId(materialsId);
				batchDetail.setBatchNo(order.getId());
				batchDetail.setPrice(orderDetail.getPrice());
				batchDetail.setType(MaterialsBatchDetail.MATERIALS_BATCH_TYPE_MATERIALS);
				if (acceptNumber > 0) {
					//这里是合格的入库数量,使用了共用表，字段名有些歧义
					//in_number 表示合格入库数量
					//out_number 表示合格出库数量
					batchDetail.setInNumber(acceptNumber);
				}
				if (otherNumber > 0) {
					//这里是不合格的入库数量,使用了共用表，字段名有些歧义
					//plan_number 表示不合格入库数量
					//number 表示不合格出库数量
					batchDetail.setPlanNumber(otherNumber);
				}
			} else {
				if (acceptNumber > 0) {
					//这里是合格的入库数量,使用了共用表，字段名有些歧义
					//in_number 表示合格入库数量
					//out_number 表示合格出库数量
					batchDetail.setInNumber(batchDetail.getInNumber() + acceptNumber);
				}
				if (otherNumber > 0) {
					//这里是不合格的入库数量,使用了共用表，字段名有些歧义
					//plan_number 表示不合格入库数量
					//number 表示不合格出库数量
					batchDetail.setPlanNumber(batchDetail.getPlanNumber() + otherNumber);
				}
			}
			materialsBatchDetailDao.save(batchDetail);
		}
		
		//更新收货单状态为入库
		order.setStatus(MaterialsReceiveOrder.ORDER_STATES_IN_STORE);
		order.setUpdateDate(DateUtils.now());
		orderDao.saveMaterialsReceiveOrder(order);
		
		//生成应付款记录
		StatementRecord record = new StatementRecord();
		record.setInOutType(StatementRecord.STATEMENT_IN_OUT_TYPE_OUT);
		record.setRecordType(StatementRecord.STATEMENT_RECORD_TYPE_ADD);
		record.setOrderId(order.getId());
		//record.setRecordDate(order.getArriveDate());
		record.setCustomerId(order.getSupplierId());
		record.setCustomerName(order.getSupplierName());
		record.setAmount(order.getTotleAmount());
		statementRecordDao.saveStatementRecord(record);
	}
}
